/**************************************************************************************
Class Name              : ConsoleHomeController_AC
Created By              : Kalyani Nemala
Created on              : 01/22/2013
Description             : Controller for the home page in the West Case Management App.
Version                 : 1.0
***************************************************************************************/
public class ConsoleHomeController_AC {
    public String idAssignedCaseFromQueue{get; set;}
    
    public void getCaseClicked(){
        
        Set<String> setQueueNames = new Set<String>();
        Set<Id> setQueueIds = new Set<Id>();
        Set<Id> setInProgressCaseAccountIds = new Set<Id>();
        Case objCaseToBeAssignedFromQueue;
        
        List<Case> lstInprogressCasesAssignedtoCurrentUser = [SELECT id,Casenumber,priority,Next_call__c FROM Case
                                                                WHERE recordtypeid = :Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('West - Support').getRecordTypeId()
                                                                AND ownerid = :userInfo.getUserId()
                                                                AND Status = 'In Progress' LIMIT 1];
                                                                
        if(lstInprogressCasesAssignedtoCurrentUser != null && lstInprogressCasesAssignedtoCurrentUser.size() > 0){
                idAssignedCaseFromQueue = lstInprogressCasesAssignedtoCurrentUser.get(0).id;
        }else{
                List<Group> lstAssignedGroups = [SELECT Id, Name, Type FROM Group WHERE id in (SELECT GroupId FROM GroupMember WHERE userorgroupid = :userInfo.getUserId()) AND Type = 'Queue'];
        
            for(Group objGroup:lstAssignedGroups){
                    setQueueNames.add(objGroup.Name);
            }
            
            List<QueueSobject> lstQueueSObject= [SELECT queueid FROM QueueSobject WHERE queue.name in :setQueueNames AND sobjectType = 'Case'];                         
            
            for(QueueSobject objQueueSObject:lstQueueSObject){
                    setQueueIds.add(objQueueSObject.queueid);
            }  
            
            List<Case> lstInProgressCasesInQueue = [SELECT AccountId,Lead__C FROM Case WHERE recordtypeid = :Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('West - Support').getRecordTypeId() AND Status = 'In Progress'];
            
            for(Case objCase:lstInProgressCasesInQueue){
                if(objCase.lead__C!= null){
                	if(!setInProgressCaseAccountIds.contains(objCase.lead__C)){
                        setInProgressCaseAccountIds.add(objCase.lead__C);
                	}	
                }
                if(objCase.AccountId!= null && !setInProgressCaseAccountIds.contains(objCase.AccountId)){
                        setInProgressCaseAccountIds.add(objCase.AccountId);
                }
            }
            
            List<Case> lstCasesWithNextCallTime = [SELECT id,Casenumber,priority,Next_call__c FROM Case
                                                                                            WHERE recordtypeid = :Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('West - Support').getRecordTypeId()
                                                                                            AND (ownerid = :userInfo.getUserId() OR ownerid IN :setQueueIds)
                                                                                            AND next_call__c <= :system.now() AND next_call__c != null
                                                                                            AND Status != 'Closed'
                                                                                            AND Accountid NOT IN :setInProgressCaseAccountIds
                                                                                            //AND Lead__c NOT IN :setInProgressCaseAccountIds
                                                                                            ORDER BY priority ASC,Next_call__c DESC];
                                                                                            
            List<Case> lstCasesWithNoFollowupCall = [SELECT id,Casenumber,priority,Next_call__c FROM Case
                                                                                                    WHERE recordtypeid = :Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('West - Support').getRecordTypeId()
                                                                                                    AND (ownerid = :userInfo.getUserId() OR ownerid IN :setQueueIds)
                                                                                                    AND next_call__c = null
                                                                                                    AND Status != 'Closed'
                                                                                                    AND Accountid NOT IN :setInProgressCaseAccountIds
                                                                                                    //AND Lead__c NOT IN :setInProgressCaseAccountIds
                                                                                                    ORDER BY priority ASC,createddate ASC];
            
            if(lstCasesWithNextCallTime != null && lstCasesWithNextCallTime.size() > 0){
                            if(lstCasesWithNoFollowupCall != null && lstCasesWithNoFollowupCall.size() > 0){
                                    if(lstCasesWithNextCallTime.get(0).priority <= lstCasesWithNoFollowupCall.get(0).priority){
                                        objCaseToBeAssignedFromQueue = lstCasesWithNextCallTime.get(0);
                                        idAssignedCaseFromQueue = lstCasesWithNextCallTime.get(0).id;
                                    }else{
                                        objCaseToBeAssignedFromQueue = lstCasesWithNoFollowupCall.get(0);
                                        idAssignedCaseFromQueue = lstCasesWithNoFollowupCall.get(0).id;
                                    }
                            }else{
                                objCaseToBeAssignedFromQueue = lstCasesWithNextCallTime.get(0);
                                idAssignedCaseFromQueue = lstCasesWithNextCallTime.get(0).id;
                            }
            }else if(lstCasesWithNoFollowupCall != null && lstCasesWithNoFollowupCall.size() > 0){
                objCaseToBeAssignedFromQueue = lstCasesWithNoFollowupCall.get(0);
                idAssignedCaseFromQueue = lstCasesWithNoFollowupCall.get(0).id;
            }else{
                idAssignedCaseFromQueue = '';
            }
            
            if(objCaseToBeAssignedFromQueue != null){
                objCaseToBeAssignedFromQueue.ownerid = userInfo.getUserId();
                objCaseToBeAssignedFromQueue.status = 'In Progress';
                objCaseToBeAssignedFromQueue.Manual_status_change__c = true;
                try{
                        update objCaseToBeAssignedFromQueue;
                }catch(Exception e){
                        system.debug(e);
                }
            }
       }
        }
}